package com.cjm.algorithm.leetcode;

public class FindMedianSortedArrays {
    public static double findMedianSortedArrays(int[] nums1, int[] nums2) {
        int i = 0;
        int j = 0;

        int[] target = new int[nums1.length + nums2.length];
        int k = 0;
        while (true) {
            if (i > nums1.length - 1 && j > nums2.length - 1) {
                break;
            }
            if (i > nums1.length - 1) {
                target[k] = nums2[j];
                j++;
                k++;
                continue;
            }

            if (j > nums2.length - 1) {
                target[k] = nums1[i];
                i++;
                k++;
                continue;
            }

            int i1 = nums1[i];
            int i2 = nums2[j];
            if (i1 > i2) {
                target[k] = i2;
                j++;
            } else {
                target[k] = i1;
                i++;
            }
            k++;
        }
        boolean isd = (nums1.length + nums2.length) % 2 == 0;
        int mid = (nums1.length + nums2.length) / 2;
        if (!isd) {
            return target[mid];
        }
        return (target[mid] + target[mid - 1]) / 2.0;
    }

    public static void main(String[] args) {
        System.err.println(findMedianSortedArrays(new int[]{1, 3}, new int[]{2}));
        System.err.println(findMedianSortedArrays(new int[]{1, 2, 3, 4, 5, 6}, new int[]{3, 4}));
    }
}
